package com.zzy.controller;

import com.zzy.model.AjaxMsg;
import com.zzy.model.TestModel;
import com.zzy.service.TestModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/test")
public class TestController {

    /**返回一个json对象*/
    @RequestMapping({"/test"})
    @ResponseBody
    public AjaxMsg test(String id) {
        System.out.println(id);
        return AjaxMsg.returnAjaxMsg(200,"测试啊");
    }

    @Autowired
    private TestModelService testModelService;

    @RequestMapping({"/list"})
    public String list() {
        return "page/test/test_index.jsp";
    }

    @RequestMapping(value="/grid")
    public void grid(TestModel model, HttpServletRequest request, HttpServletResponse response){
        testModelService.getList(model,request,response);
    }

    /**返回一个json对象*/
    @RequestMapping({"/info"})
    @ResponseBody
    public AjaxMsg getById(String id) {
        return AjaxMsg.returnAjaxMsg(testModelService.getByID(id));
    }


    @RequestMapping({"/jump"})
    public String goInfoLayerUI(String id, HttpServletResponse response, HttpServletRequest request) {
        TestModel model = testModelService.getByID(id);
        request.setAttribute("model", model);
        request.setAttribute("load", request.getParameter("load"));
        return "page/test/test_info.jsp";
    }

    @ResponseBody
    @RequestMapping({"/delete"})
    public AjaxMsg delete(String id) {
        if (id != null) {
            String[] arr = id.split(",");
            if(arr!=null && arr.length>0){
                for(String thisid : arr ){
                    testModelService.delete(thisid);
                }
            }
        }
        return AjaxMsg.returnAjaxMsg();
    }

    @RequestMapping({"/opt"})
    @ResponseBody
    public AjaxMsg addOrUpdate(TestModel model) {
        testModelService.saveOrUpdate(model);
        return AjaxMsg.returnAjaxMsg();
    }

}
